package com.example.lynandroid.adapter

import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.lynandroid.*
import com.example.lynandroid.activity.BootActivity
import com.example.lynandroid.activity.ContainActivity
import com.example.lynandroid.activity.WindowActivity
import com.example.lynandroid.bean.DirectoryBean
import com.example.lynandroid.values.Sources

class DirectoryAdapter(
    private val activity: FragmentActivity,
    private val list: MutableList<DirectoryBean>
) :
    RecyclerView.Adapter<DirectoryAdapter.DirViewHolder>() {
    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DirViewHolder {
        return DirViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_directory, parent, false)
        )
    }

    override fun onBindViewHolder(holder: DirViewHolder, position: Int) {
        holder.text.text = list[position].name
        holder.text.typeface = Sources.loveTtf
        Glide.with(holder.itemView).load(list[position].img).into(holder.img)
        colorList?.let {
            holder.text.setTextColor(it.getColor(position % it.length(), Color.LTGRAY))
        }
        holder.itemView.setOnClickListener {
            val data = list[position]
            when (data.fragment) {
                ACTIVITY_BOOT -> {
                    Intent(activity, BootActivity::class.java).apply {
                        activity.startActivity(this)
                    }
                }
                ACTIVITY_WINDOW -> {
                    Intent(activity, WindowActivity::class.java).apply {
                        activity.startActivity(this)
                    }
                }
                else -> {
                    Intent(activity, ContainActivity::class.java).apply {
                        putExtra(KEY_FRAGMENT_CLASS, data.fragment)
                        activity.startActivity(this)
                    }
                }
            }

        }
    }


    class DirViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val text: TextView = view.findViewById(R.id.text)
        val img: ImageView = view.findViewById(R.id.img)
    }


}